from django.db import models

# Create your models here.
from shops.models import ShopsModel,GoodsModel
from send.models import SendModel
# from order.models import OrderModel

# 会员表
class RoleModel(models.Model):
    name = models.CharField(verbose_name="名称", max_length=20)
    discount = models.DecimalField(verbose_name="折扣", max_digits=3,decimal_places=2)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)
    class Meta():
        db_table="role_tb"
        verbose_name = "会员表"
        verbose_name_plural = verbose_name
    def __str__(self):
        return "%s 折扣 %s"%(self.name,self.discount)


# 用户表
class UserModel(models.Model):
    nickname = models.CharField(verbose_name="昵称", max_length=20)
    avatar_url = models.CharField(verbose_name="用户头像", max_length=500, null=True)
    mobile = models.CharField(verbose_name='手机号', max_length=11)
    password = models.CharField(verbose_name="密码", max_length=256)
    role = models.ForeignKey(verbose_name="vip", to=RoleModel, on_delete=models.PROTECT)
    date_joined = models.DateTimeField(verbose_name="注册时间", auto_now_add=True)
    last_login = models.DateTimeField(verbose_name="上次登录时间", null=True)
    is_staff = models.BooleanField(verbose_name="是否是管理员", default=False)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)
    class Meta():
        db_table="users_tb"
        verbose_name = "用户表"
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.nickname

# 地址表
class AddressModel(models.Model):
    user = models.ForeignKey(verbose_name="用户",to=UserModel, on_delete=models.CASCADE)
    nickname = models.CharField(verbose_name="收货人",max_length=20)
    mobile = models.CharField(verbose_name='手机号', max_length=11)
    address = models.CharField(verbose_name="地址",max_length=128)
    is_default = models.BooleanField(verbose_name="是否默认", default=True)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)
    class Meta():
        db_table="address_tb"
        verbose_name = "地址表"
        verbose_name_plural = verbose_name
    def __str__(self):
        return "%s 地址 %s"%(self.user.nickname,self.address)


# 收藏表
class CollectModel(models.Model):
    user = models.ForeignKey(verbose_name="用户", to=UserModel, on_delete=models.CASCADE)
    shops = models.ForeignKey(verbose_name="商家", to=ShopsModel, on_delete=models.CASCADE)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)
    class Meta():
        db_table="collect_tb"
        verbose_name = "收藏表"
        verbose_name_plural = verbose_name
    def __str__(self):
        return "%s 收藏 %s"%(self.user.nickname,self.shops.nickname)


# 优惠卷表
class CouponsModel(models.Model):
    COUPONS_TYPE = (
        (1, "商家"),
        (2, "平台"),
    )
    type = models.SmallIntegerField(verbose_name="优惠卷类型", choices=COUPONS_TYPE)
    name = models.CharField(verbose_name="名称", max_length=20)
    valid_time = models.DateTimeField(verbose_name="有效期")
    free_price = models.DecimalField(verbose_name="面额", max_digits=6, decimal_places=2)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)

    class Meta():
        db_table = "coupons_tb"
        verbose_name = "优惠卷表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s 面额 %s 有效期 %s" % (self.name, self.free_price, self.valid_time)


# 用户优惠卷表
class UserCouponsModel(models.Model):
    user = models.ForeignKey(verbose_name="用户", to=UserModel, on_delete=models.CASCADE)
    coupons = models.ForeignKey(verbose_name="优惠卷", to=CouponsModel, on_delete=models.CASCADE)
    is_use = models.BooleanField(verbose_name="是否使用", default=False)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)
    class Meta():
        db_table = "user_coupons_tb"
        verbose_name = "用户优惠卷表"
        verbose_name_plural = verbose_name
    def __str__(self):
        return "%s 优惠卷 %s 有效期 %s 是否使用 %s" % (self.user.nickname, self.coupons.name, self.coupons.free_price,self.is_use)



# 沟通表
class CommunicationModel(models.Model):
    user = models.ForeignKey(verbose_name="用户", to=UserModel, on_delete=models.CASCADE)
    shops = models.ForeignKey(verbose_name="商家", to=ShopsModel, on_delete=models.CASCADE)
    send = models.ForeignKey(verbose_name='骑手', to=SendModel, on_delete=models.CASCADE)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)
    class Meta():
        db_table = "communication_tb"
        verbose_name = "沟通表"
        verbose_name_plural = verbose_name
    def __str__(self):
        return "用户 %s 商家 %s" % (self.user.nickname, self.shops.nickname)

# 消息表
class MsgModel(models.Model):
    msg_sender = models.ForeignKey(verbose_name="发送者", to=UserModel, on_delete=models.CASCADE)
    msg_content = models.CharField(verbose_name="消息内容",max_length=256)
    msg_create_datetiem = models.DateTimeField(verbose_name="发送时间", auto_now_add=True)
    group = models.ForeignKey(verbose_name="所属群聊", to=CommunicationModel, on_delete=models.CASCADE)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)
    class Meta():
        db_table = "msg_tb"
        verbose_name = "消息表"
        verbose_name_plural = verbose_name
    def __str__(self):
        return "%s 发送 %s 时间 %s" % (self.msg_sender.nickname, self.msg_content,self.msg_create_datetiem)

# 订单
class OrderModel(models.Model):
    ORDER_STATE = (
        (1, "未支付"),
        (2, "已支付"),
        (3, "待接单"),
        (4, "已接单"),
        (5, "已送达"),
    )

    PAY_METHOD = (
        (1, "支付宝"),
        (2, "微信"),
    )
    user = models.ForeignKey(verbose_name="用户", to=UserModel, on_delete=models.PROTECT)
    shops = models.ForeignKey(verbose_name="商家", to=ShopsModel, on_delete=models.PROTECT)
    send = models.ForeignKey(verbose_name='骑手', to=SendModel, on_delete=models.CASCADE, null=True)
    sum = models.IntegerField(verbose_name="总数量")
    total_amount = models.DecimalField(verbose_name="总金额", max_digits=7, decimal_places=2)
    remark = models.CharField(verbose_name="备注", max_length=128,null=True)
    order_id = models.CharField(verbose_name="订单id",max_length=256)
    trade_no = models.CharField(verbose_name="支付订单号",max_length=512,null=True)
    pay_method = models.SmallIntegerField(verbose_name="支付方式", choices=PAY_METHOD)
    pay_state = models.SmallIntegerField(verbose_name="订单状态", choices=ORDER_STATE, default=1)
    pay_time = models.DateTimeField(verbose_name="下单时间", auto_now_add=True)
    address = models.ForeignKey(verbose_name="收货地址", to=AddressModel, on_delete=models.PROTECT)
    coupons = models.ForeignKey(verbose_name="优惠卷", to=UserCouponsModel, on_delete=models.PROTECT,null=True)
    is_refund = models.BooleanField(verbose_name="是否退款", default=False)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)
    class Meta():
        db_table="order_tb"
        verbose_name = "订单表"
        verbose_name_plural = verbose_name
    def __str__(self):
        return "订单 %s %s 商家 %s"%(self.order_id,self.user.nickname,self.shops)

# 订单商品表
class OrderGoodsModel(models.Model):
    order = models.ForeignKey(verbose_name="订单", to=OrderModel, on_delete=models.CASCADE)
    goods = models.ForeignKey(verbose_name="商品", to=GoodsModel, on_delete=models.CASCADE)
    sum = models.IntegerField(verbose_name="数量")
    sum_price = models.DecimalField(verbose_name="总价格", max_digits=7, decimal_places=2)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)
    class Meta():
        db_table="order_goods_tb"
        verbose_name = "订单商品表"
        verbose_name_plural = verbose_name
    def __str__(self):
        return "%s 商品 %s "%(self.order.order_id,self.goods.name)


# 评论表
class CommentModel(models.Model):
    create_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True, null=True)
    user = models.ForeignKey(verbose_name="用户", to=UserModel, on_delete=models.CASCADE)
    shops = models.ForeignKey(verbose_name="商家", to=ShopsModel, on_delete=models.CASCADE)
    order = models.ForeignKey(verbose_name="订单", to=OrderModel, on_delete=models.CASCADE,blank=True , null=True)

    content = models.CharField(verbose_name="评论内容", max_length=128)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)
    class Meta():
        db_table="comment_tb"
        verbose_name = "收藏表"
        verbose_name_plural = verbose_name
    def __str__(self):
        return "%s 收藏 %s"%(self.user.nickname,self.shops.nickname)

# 回复表
class ReplyModel(models.Model):
    create_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True, null=True)
    content = models.CharField(verbose_name='回复内容',max_length=128)
    comment = models.ForeignKey(verbose_name='回复的主评论',to=CommentModel, on_delete=models.CASCADE)
    user = models.ForeignKey(verbose_name="用户", to=UserModel, on_delete=models.CASCADE , null=True)
    shops = models.ForeignKey(verbose_name="商家", to=ShopsModel, on_delete=models.CASCADE , null=True)
    reply = models.IntegerField(verbose_name='被回复的人', null=True)


    class Meta():
        db_table = 'reply_tb'
        verbose_name = '回复表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return  self.content
